package com.zhou.hot.script;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Properties;

@Slf4j
public class ScriptConfig {

	static Properties properties =new Properties();
	static boolean isLoaded;
	public static void loadProperties() {
		if (isLoaded) {
			return;
		}
		isLoaded = true;
		Properties temp = new Properties();
		URL url = ScriptConfig.class.getClassLoader().getResource("script.properties");
		if (url == null) {
			return;
		}
		String fileName = url.getPath();
		try
		{
			
			log.info("<<<<<<<<<<<<<load properties:"+fileName);
			File file = new File(fileName);
			temp.load(new FileInputStream(file));
			properties = temp;
			log.info("Game Properties loaded!");
			
		}
		catch (Exception e)
		{
			log.error("ERROR IN LOAD "+fileName, e);
		}
	}
	
	public static String getString(String key,String default_value) 
	{
		String value = default_value;
		try {
			String t = properties.getProperty(key);
			if (t == null || t.isEmpty()){
				return value;
			}
			value = t;
		} catch (Exception e) {
			log.error("getString Key["+key+"] is error",e);
		}
		return value;
	}
	
	public static int getInt(String key,int default_value) 
	{
		int value = default_value;
		try {
			String t = properties.getProperty(key);
			if (t == null || t.isEmpty()){
				return value;
			}
			value = Integer.valueOf(t);
		} catch (Exception e) {
			log.error("getInt Key["+key+"] is error",e);
		}
		return value;
	}
	
	public static String[] getStringArrays(String key,String regex) 
	{
		try {
			String t = properties.getProperty(key);
			if (t == null || t.isEmpty()) {
				return null;
			}
			return t.split(regex);
		} catch (Exception e) {
			log.error("getStringArrays Key["+key+"] is error",e);
			return null;
		}
	}

	
	public Properties getProperties() {
		return properties;
	}


	public static void main(String[] args) {
		String name =" ";
		try {
			System.out.println(new String(name.getBytes("unicode"), "unicode"));
			for (char c : name.toCharArray()) {
				System.out.print("\\u"+Integer.toHexString(c));
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
